import { Component } from 'react';
import { Button, Card, Checkbox, Form, Input, message } from 'antd';
import styles from './index.module.scss';
import imgSrc from 'assets/logo.png';
import { loginAPI } from 'api/user';
import { setToken } from 'utils/storage';
console.log('styles  ----->  ', styles);
export default class Login extends Component {
  onFinish = async (values) => {
    console.log(values);
    // 4. 调用登录接口
    try {
      // 错误请求来源于发起请求这里
      const res = await loginAPI(values);
      console.log('res', res);
      setToken(res.data.token);
      this.props.history.push('/');
      // 提示用户
      message.success('登录成功', 1);
    } catch (error) {
      console.dir(error);
      message.error(error.response.data.message);
    }
  };
  render() {
    return (
      <div className={styles.login}>
        <h1 className="color">我会得到一个红色</h1>
        <Card className="login-container">
          <img src={imgSrc} alt="" className="login-logo" />
          <Form
            autoComplete="off"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                {
                  required: true,
                  message: '手机号不能为空!',
                },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '请输入11位的手机号码',
                },
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  required: true,
                  message: '验证码不能为空!',
                },
                {
                  pattern: /^\d{6}$/,
                  message: '请输入6位验证码',
                },
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              valuePropName="checked"
              name="agree"
              rules={[
                {
                  validator: (_, value) =>
                    value
                      ? Promise.resolve()
                      : Promise.reject(new Error('请同意并勾选')),
                },
                //                 {validator: (_, value) =>{
                // if (value === true) {
                //   return Promise.resolve();
                // }else{
                //   return Promise.reject(new Error('请同意并勾选'))
                // }
                //                 }
              ]}
            >
              <Checkbox>我已阅读并同意[隐私条款]和[用户协议]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    );
  }
}
